How to Add A New Command
Sunday, February 19, 2023
12:05 PM
Commands are categorized into functional domains. These domains are described by the folders beneath the Commands folder, for example Clean, Edit, File, Images, etc. When inventing a new command, it's important to identify the proper domain. If this command doesn't fit an existing domain, consider whether it can be added to the Extras domain, the Tools domain, or if is the beginning of multiple related commands, create a new domain folder to contain this and other related commands.
|
To add a new command to OneMore
- Add a new class file in the appropriate Commands\domain folder.
- Name the class using form nameCommand such as TrimCommand
- The file name should match the class name such as TrimCommand.cs.
- Keep the namespace as River.OneMoreAddIn.Commands
- Derive the class from the Command base class and declare it as internal
- Add a public constructor, even if its body is blank
- Override the Command.Execute method
Example:
namespace River.OneMoreAddIn.Commands
{
internal class TrimCommand : Command
{
public TrimCommand()
{
}
public override async Task Execute(params object[] args)
{
}
}
}
- Add a new method to the AddinCommands.cs file to invoke the command
- The name should be derived from the class, replacing Command with Cmd such as TrimCmd
- It must be declared async and accept an IRibbonControl parameter
- It must invoke the command using factory.Run as shown here
[Command("ribTrimButton_Label", Keys.None, "ribCleanMenu")]
public async Task TrimCmd(IRibbonControl control)
=> await factory.Run<TrimCommand>(false);
- The CommandAttribute first parameter specifies the resource ID used to translate the name of the command. This must be named using the form ribNameButton_Label
- The second parameter defines the default key binding; set to Keys.None to indicate to binding
- The third parameter names the functional domain (for future use)
- Add a button control or menu item to the Ribbon\Ribbon.xml file
- Specify a unique id and label property. The id should match the CommandAttribute resource name with the _Label part, for example ribTrimButton
- Choose an appropriate imageMso name from imageMso List
- Set the onAction property to the nameCmd method added to AddInCommands.cs
<button
id="ribTrimButton"
imageMso="AutoTextGallery"
getLabel="GetRibbonLabel"
onAction="TrimCmd"/>
#omwiki #omdeveloper
© 2020 Steven M Cohn. All rights reserved.
Please consider a sponsorship or one-time donation to support ongoing development
Created with OneNote.